Rania Abdul Hafiz
  • Home
  • Mini Project 1A: Static Maps
  • Mini Project 1B: Interactive Maps
  • Mini Project 2:Data Scraping
  • Mini Project 4: Text Analysis

On this page

  • R Markdown

Mini Project 1B: Interactive Maps

Published

September 20, 2024

library(sf)
states <- read_sf("https://rstudio.github.io/leaflet/json/us-states.geojson")
class(states)
states
[1] "sf"         "tbl_df"     "tbl"        "data.frame"
Simple feature collection with 52 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -188.9049 ymin: 17.92956 xmax: -65.6268 ymax: 71.35163
Geodetic CRS:  WGS 84
# A tibble: 52 × 4
   id    name                  density                                  geometry
   <chr> <chr>                   <dbl>                        <MULTIPOLYGON [°]>
 1 01    Alabama                 94.6  (((-87.3593 35.00118, -85.60667 34.98475…
 2 02    Alaska                   1.26 (((-131.602 55.11798, -131.5692 55.28229…
 3 04    Arizona                 57.0  (((-109.0425 37.00026, -109.048 31.33163…
 4 05    Arkansas                56.4  (((-94.47384 36.50186, -90.15254 36.4963…
 5 06    California             242.   (((-123.2333 42.00619, -122.3789 42.0116…
 6 08    Colorado                49.3  (((-107.9197 41.00391, -105.729 40.99843…
 7 09    Connecticut            739.   (((-73.05353 42.03905, -71.79931 42.0226…
 8 10    Delaware               464.   (((-75.41409 39.80446, -75.5072 39.68396…
 9 11    District of Columbia 10065    (((-77.03526 38.99387, -76.90929 38.8952…
10 12    Florida                353.   (((-85.49714 30.99754, -85.00421 31.0030…
# ℹ 42 more rows

R Markdown

  1. An Interactive Numeric Map: The Highest Violent Crime Rate Type in each State.
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(leaflet)
library(shiny)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ readr     2.1.5
✔ ggplot2   3.5.1     ✔ stringr   1.5.1
✔ lubridate 1.9.3     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
crime <- read.csv("https://corgis-edu.github.io/corgis/datasets/csv/state_crime/state_crime.csv")

violent_crimes <- crime

states <- states %>%
   left_join(violent_crimes, 
             by = c("name" = "State"))

violent_crimes <- violent_crimes %>%
  mutate(State = tolower(State))

data_mapped <- violent_crimes %>%
   left_join(states, 
             by = c("State" = "name"))

states_map <- map_data("state")


# Summarize violent crimes by type
violent_crimes <- crime %>%
  group_by(State) %>% 
  summarize(Data.Totals.Violent.Assault = sum(Data.Totals.Violent.Assault),
            Data.Totals.Violent.Rape = sum(Data.Totals.Violent.Rape),
            Data.Totals.Violent.Robbery = sum(Data.Totals.Violent.Robbery),
            Data.Totals.Violent.Murder = sum(Data.Totals.Violent.Murder)) %>% 
  select(State, 
          Data.Totals.Violent.Assault, 
          Data.Totals.Violent.Rape, 
          Data.Totals.Violent.Robbery, 
          Data.Totals.Violent.Murder) %>%
   mutate(Highest_Crime = pmax(Data.Totals.Violent.Assault, 
                               Data.Totals.Violent.Rape, 
                               Data.Totals.Violent.Robbery,
                               Data.Totals.Violent.Murder, na.rm = TRUE)) %>%
   mutate(Highest_Crime_Type = case_when(
     Highest_Crime == Data.Totals.Violent.Assault ~ "Assault",
     Highest_Crime == Data.Totals.Violent.Rape ~ "Rape",
     Highest_Crime == Data.Totals.Violent.Robbery ~ "Robbery",
     Highest_Crime == Data.Totals.Violent.Murder ~ "Murder",
     TRUE ~ NA_character_
   )) %>%
   select(State, 
          Highest_Crime_Type)


data_mapped <- violent_crimes %>%
   left_join(states,
             by = c("State" = "name"))
 
states <- states %>%
   left_join(violent_crimes, by = c("name" = "State"))


# Color mapping for different crime types
states <- states %>%
   mutate(c = case_when(
      Highest_Crime_Type == "Assault" ~ "darkred",
      Highest_Crime_Type == "Rape" ~ "white",
      Highest_Crime_Type == "Robbery" ~ "lightyellow",
      Highest_Crime_Type == "Murder" ~ "white",
      TRUE ~ "grey"
))
 

violent_crimes <- violent_crimes %>%
   mutate(State = tolower(State))


# leaflet  
leaflet(states) %>%
   setView(lng = -96, 
           lat = 37.8, 
           zoom = 4) %>% 
   addTiles() %>%
   addPolygons(
     fillColor = states$c,
     weight = 1,
     opacity = 1,
     color = "black",
     dashArray = "3",
     fillOpacity = 0.7,
     highlight = highlightOptions(
       weight = 2,
       color = "white",
       dashArray = "",
       fillOpacity = 0.7,
       bringToFront = TRUE
     ),
     label = paste(states$name, ":", states$Highest_Crime_Type),
     labelOptions = labelOptions(
       style = list("font-weight" = "normal", 
                    padding = "3px 8px"),
       textsize = "15px",
       direction = "auto"))
Warning in sf::st_is_longlat(x): bounding box has potentially an invalid value
range for longlat data

Description: An interactive numeric map of the United States (excluding Alaska and Hawaii) which visualizes the most common violent crime by state, with different colors representing types of crimes: red for assault, blue for rape, green for robbery, purple for murder, and grey where data is unavailable. The map labels each state with the crime type it experiences most frequently. The aim is to visualize the geographical distribution of violent crimes across the country, which helps in identify regional patterns that might influence decisions on crime prevention and safety precautions.